﻿using LiteBlog.Common.Entities;
using LiteBlog.Components;
using LiteBlog.Components.Contracts;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;

namespace LiteBlog.Controllers.Helpers
{
    internal class SkinHelper
    {
        private static readonly string SkinAppVarName = "Skin";
        private static readonly string DefaultSkin = "Green";

        internal static Color GetSkinColor(string skin)
        {
            Skin skinEnum = Skin.Green;
            Enum.TryParse<Skin>(skin, out skinEnum);
            switch (skinEnum)
            {
                case Skin.Red:
                    return Color.FromArgb(0xED, 0x1C, 0x24);
                case Skin.Green:
                    return Color.FromArgb(0x00, 0x9E, 0x49);
                case Skin.Blue:
                    return Color.FromArgb(0x00, 0x70, 0xBB);
                case Skin.Gray:
                    return Color.FromArgb(0x80, 0x80, 0x80);
                default:
                    return Color.White;
            }
        }

        internal static List<KeyValuePair<string, string>> GetVariablesForBlog(string skin)
        {
            Skin skinEnum = (Skin)Enum.Parse(typeof(Skin), skin);
            var variables = new List<KeyValuePair<string, string>>();
            switch (skinEnum)
            {
                case Skin.Red:
                    variables.Add(new KeyValuePair<string, string>("@@@primary@@@", "#ED1C24"));
                    variables.Add(new KeyValuePair<string, string>("@@@hover@@@", "yellow"));
                    variables.Add(new KeyValuePair<string, string>("@@@title@@@", "#2778ec"));
                    variables.Add(new KeyValuePair<string, string>("@@@comment@@@", "#FFF0F5"));
                    break;
                case Skin.Green:
                    variables.Add(new KeyValuePair<string, string>("@@@primary@@@", "#009E49"));
                    variables.Add(new KeyValuePair<string, string>("@@@hover@@@", "lawngreen"));
                    variables.Add(new KeyValuePair<string, string>("@@@title@@@", "#2778ec"));
                    variables.Add(new KeyValuePair<string, string>("@@@comment@@@", "#F0FFF0"));
                    break;
                case Skin.Blue:
                    variables.Add(new KeyValuePair<string, string>("@@@primary@@@", "#0070BB"));
                    variables.Add(new KeyValuePair<string, string>("@@@hover@@@", "lightblue"));
                    variables.Add(new KeyValuePair<string, string>("@@@title@@@", "#2778ec"));
                    variables.Add(new KeyValuePair<string, string>("@@@comment@@@", "#CAE1FF"));
                    break;
                case Skin.Gray:
                    variables.Add(new KeyValuePair<string, string>("@@@primary@@@", "#808080"));
                    variables.Add(new KeyValuePair<string, string>("@@@hover@@@", "silver"));
                    variables.Add(new KeyValuePair<string, string>("@@@title@@@", "#2778ec"));
                    variables.Add(new KeyValuePair<string, string>("@@@comment@@@", "#F5F5DC"));
                    break;
                default:
                    variables.Add(new KeyValuePair<string, string>("@@@primary@@@", "#009E49"));
                    variables.Add(new KeyValuePair<string, string>("@@@hover@@@", "lawngreen"));
                    variables.Add(new KeyValuePair<string, string>("@@@title@@@", "#2778ec"));
                    break;
            }

            return variables;
        }

        internal static List<KeyValuePair<string, string>> GetVariablesForAdmin(string skin)
        {
            Skin skinEnum = (Skin)Enum.Parse(typeof(Skin), skin);
            var variables = new List<KeyValuePair<string, string>>();
            switch (skinEnum)
            {
                case Skin.Red:
                    variables.Add(new KeyValuePair<string, string>("@@@primary@@@", "#ED1C24"));
                    variables.Add(new KeyValuePair<string, string>("@@@hover@@@", "yellow"));
                    variables.Add(new KeyValuePair<string, string>("@@@border@@@", "red"));
                    variables.Add(new KeyValuePair<string, string>("@@@alternate@@@", "lightcoral"));
                    variables.Add(new KeyValuePair<string, string>("@@@dark@@@", "yellow"));
                    variables.Add(new KeyValuePair<string, string>("@@@title@@@", "#2778ec"));
                    break;
                case Skin.Green:
                    variables.Add(new KeyValuePair<string, string>("@@@primary@@@", "#009E49"));
                    variables.Add(new KeyValuePair<string, string>("@@@hover@@@", "lawngreen"));
                    variables.Add(new KeyValuePair<string, string>("@@@border@@@", "#44bb44"));
                    variables.Add(new KeyValuePair<string, string>("@@@alternate@@@", "lightgreen"));
                    variables.Add(new KeyValuePair<string, string>("@@@dark@@@", "green"));
                    variables.Add(new KeyValuePair<string, string>("@@@title@@@", "#2778ec"));
                    break;
                case Skin.Blue:
                    variables.Add(new KeyValuePair<string, string>("@@@primary@@@", "#0070BB"));
                    variables.Add(new KeyValuePair<string, string>("@@@hover@@@", "lightblue"));
                    variables.Add(new KeyValuePair<string, string>("@@@border@@@", "blue"));
                    variables.Add(new KeyValuePair<string, string>("@@@alternate@@@", "lightblue"));
                    variables.Add(new KeyValuePair<string, string>("@@@dark@@@", "navy"));
                    variables.Add(new KeyValuePair<string, string>("@@@title@@@", "#2778ec"));
                    break;
                case Skin.Gray:
                    variables.Add(new KeyValuePair<string, string>("@@@primary@@@", "#808080"));
                    variables.Add(new KeyValuePair<string, string>("@@@hover@@@", "silver"));
                    variables.Add(new KeyValuePair<string, string>("@@@border@@@", "darkgray"));
                    variables.Add(new KeyValuePair<string, string>("@@@alternate@@@", "lightgray"));
                    variables.Add(new KeyValuePair<string, string>("@@@dark@@@", "darkgray"));
                    variables.Add(new KeyValuePair<string, string>("@@@title@@@", "#2778ec"));
                    break;
                default:
                    variables.Add(new KeyValuePair<string, string>("@@@primary@@@", "#009E49"));
                    variables.Add(new KeyValuePair<string, string>("@@@hover@@@", "lawngreen"));
                    variables.Add(new KeyValuePair<string, string>("@@@border@@@", "#44bb44"));
                    variables.Add(new KeyValuePair<string, string>("@@@alternate@@@", "lightgreen"));
                    variables.Add(new KeyValuePair<string, string>("@@@dark@@@", "green"));
                    variables.Add(new KeyValuePair<string, string>("@@@title@@@", "#2778ec"));
                    break;
            }

            return variables;
        }

        internal static string GetSkin()
        {
            string skin = DefaultSkin;
            if (HttpContext.Current != null)
            {
                var request = HttpContext.Current.Request;
                if (request != null && request.Cookies != null)
                {
                    var cookie = request.Cookies["Skin"];
                    if (cookie != null)
                        skin = cookie.Value;
                }
            }
            
            if (HttpContext.Current!=null && string.IsNullOrEmpty(skin))
                skin = (string)HttpContext.Current.Application[SkinAppVarName];

            return skin;
        }
    }
}